using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.ProductionManage;
using ProduceBasicData.Api.DTO.ProductionManage;
using ProduceBasicData.Domain.ProductionManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.ProductionManage
{
    public class GetProductCommandHandler : IRequestHandler<GetProductCommand, ApiResult<List<Productdto>>>
    {
        private readonly IBaseRespository<ProductMange> _product;
        private readonly IMapper _mapper;

        public GetProductCommandHandler(IBaseRespository<ProductMange> product, IMapper mapper)
        {
            _product = product;
            _mapper = mapper;
        }

        public Task<ApiResult<List<Productdto>>> Handle(GetProductCommand request, CancellationToken cancellationToken)
        {
            var result=new ApiResult<List<Productdto>>()
            {
                Code=ApiEnum.Success,
                Msg="获取产品成功"
            };
            var lists = _product.GetAll().Where(o => !o.Isdel).ToList();
            var info=_mapper.Map<List<Productdto>>(lists);

            result.Data = info;

            return Task.FromResult(result);
        }
    }
}
